﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BrnMall.Manager.Domain.BusinessCard;
using PetaPoco;

namespace BrnMall.Manager.Manager.BusinessCard
{
    /// <summary>
    /// 标签管理
    /// </summary>
    public static class UserTagManager
    {
        /// <summary>
        /// 获取系统内置标签
        /// </summary>
        /// <param name="tagType"></param>
        /// <returns></returns>
        public static IList<Tag> GetTags(int tagType = -1)
        {
            using (var db = new QDMallDatabase())
            {
                var sql = new Sql();
                sql.Select("*");
                sql.From("bc_tag");
                if (tagType != -1)
                    sql.Where("tagtype=@0", tagType);
                return db.Fetch<Tag>(sql);
            }
        }

        /// <summary>
        /// 获取用户标签
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="top">前几条</param>
        /// <returns></returns>
        public static IList<UserTag> GetUserTags(int uid,int top=-1)
        {
            using (var db = new QDMallDatabase())
            {
                var sql = new Sql();
                if (top != -1)
                    sql.Select($"top {top} *");
                else
                    sql.Select("*");
                sql.From("bc_usertag");
                sql.Where($"uid={uid}");
                sql.OrderBy("hitcount desc");
                
                return db.Fetch<UserTag>(sql);
            }
        }
        /// <summary>
        /// 新增用户标签
        /// </summary>
        /// <param name="tag"></param>
        public static void InsertUserTag(UserTag tag)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(tag);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static UserTag GetUserTagByName(int uid, string name)
        {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<UserTag>("select * from bc_usertag where uid=@0 and name=@1", uid, name);
            }
        }

        /// <summary>
        /// 删除用户标签
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteUserTag(int id)
        {
            using (var db = new QDMallDatabase())
            {
                try
                {
                    db.BeginTransaction();
                    db.Execute("delete from bc_usertaghit where usertagid=@0", id);
                    db.Execute("delete from bc_usertag where id=@0", id);
                    db.CompleteTransaction();
                }
                catch (Exception e)
                {
                    db.AbortTransaction();
                    throw;
                }

            }
        }
        /// <summary>
        /// 赞同用户标签
        /// </summary>
        /// <param name="hit"></param>
        public static void InsertUserTagHit(UserTagHit hit)
        {
            using (var db = new QDMallDatabase())
            {
                db.Insert(hit);
                db.Execute("update bc_usertag set hitcount=hitcount+1 where id=@0",hit.UserTagId);
            }
        }

        public static Page<UserTagHit> GetUserTagHitPage(int userTagId, long pageNumber, long itemsPerPage)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Page<UserTagHit>(pageNumber, itemsPerPage, $"select * from bc_usertaghit where usertagid={userTagId} ");
            }
        }
    }
}
